<script setup lang="ts">
import RoomStatus from './components/RoomStatus.vue'
import RoomAction from './components/RoomAction.vue'
import RoomMessage from './components/RoomMessage.vue'
import io, { Socket } from 'socket.io-client'
import { onMounted, onUnmounted } from 'vue'
import { baseURL } from '@/utils/request'
import { useUserStore } from '@/stores'
import { useRoute } from 'vue-router'
import type { Message, TimeMessages } from '@/types/room'
import { MsgType, OrderType } from '@/enums'
import { ref, nextTick, provide } from 'vue'
import type { ConsultOrderItem, Image } from '@/types/consult'
import { getConsultOrderDetail } from '@/services/consult'
import dayjs from 'dayjs'
import { showToast } from 'vant'

const consult = ref<ConsultOrderItem>()
const loadConsult = async () => {
  const res = await getConsultOrderDetail(route.query.orderId as string)
  consult.value = res.data
}

// 提供问诊订单数据给后代组件
provide('consult', consult)
const completeEva = (score: number) => {
  const item = list.value.find((item) => item.msgType === MsgType.CardEvaForm)
  if (item) {
    item.msg.evaluateDoc = { score }
    item.msgType = MsgType.CardEva
  }
}
provide('completeEva', completeEva)

const store = useUserStore()
const route = useRoute()
const list = ref<Message[]>([])
const initialMsg = ref(true)
let socket: null
const userid = route.query.id
onMounted(() => {
  // loadConsult()
  socket = new WebSocket("ws://localhost:8000/room/"+userid+"/");

// 当客户端和服务端刚创建好连接(self.accept)之后，自动触发.
 socket.onopen = function (event){
   console.log('连接成功')
  
}

// 回调函数，客户端接收服务端消息
socket.onmessage = function (event){
 
  list.value?.push({"id":1,'msgType':1,'from':'1','fromAvatar':'2','to':'1','toAvatar':'1','createTime':'2020','msg':{'content':event.data}})
  
}

// 当断开连接时，触发该函数
socket.onclose =function (event){
  console.log('关闭连接')
}


})

// 发送文字信息
const onSendText = (text: string) => {
  list.value?.push({"id":1,'msgType':1,'from':'1','fromAvatar':'2','to':'1','toAvatar':'1','createTime':'2020','msg':{'content':text}})
  socket.send(JSON.stringify({'message':text,'from':userid}))
}

// 发送图片信息
const onSendImage = (image: Image) => {
  // socket.emit('sendChatMsg', {
  //   from: store.user?.id,
  //   to: consult.value?.docInfo?.id,
  //   msgType: MsgType.MsgImage,
  //   msg: {
  //     picture: image
  //   }
  // })
}

// 下拉刷新
const loading = ref(false)
const time = ref(dayjs().format('YYYY-MM-DD HH:mm:ss'))
const onRefresh = () => {
  socket.emit('getChatMsgList', 20, time.value, consult.value?.id)
}
</script>

<template>
  <div class="room-page">
    <cp-nav-bar title="问诊室"></cp-nav-bar>
    <!-- 状态栏 -->
    <room-status
      :status="consult?.status"
      :countdown="consult?.countdown"
    ></room-status>
    <van-pull-refresh v-model="loading" @refresh="onRefresh">
      <!-- 消息 -->
      <room-message
        v-for="item in list"
        :key="item.id"
        :item="item"
      ></room-message>
    </van-pull-refresh>
    <!-- 操作栏 -->
    <room-action
      @send-text="onSendText"
      @send-image="onSendImage"
     
    ></room-action>
  </div>
</template>

<style lang="scss" scoped>
.room-page {
  padding-top: 90px;
  padding-bottom: 60px;
  min-height: 100vh;
  box-sizing: border-box;
  background-color: var(--cp-bg);
  .van-pull-refresh {
    width: 100%;
    min-height: calc(100vh - 150px);
  }
}
</style>
